package com.yunxin.admin.system.application.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.CharSequenceUtil;
import com.yunxin.admin.system.application.DictApplicationService;
import com.yunxin.admin.system.application.assembler.DictAssembler;
import com.yunxin.admin.system.application.command.AddDictCommand;
import com.yunxin.admin.system.application.command.AddDictTypeCommand;
import com.yunxin.admin.system.application.command.UpdateDeptCommand;
import com.yunxin.admin.system.application.command.UpdateDictTypeCommand;
import com.yunxin.admin.system.domain.model.dict.Dict;
import com.yunxin.admin.system.domain.model.dict.DictRepository;
import com.yunxin.admin.system.domain.model.dict.DictType;
import com.yunxin.admin.system.domain.model.dict.DictTypeRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 数据字典项业务实现类
 *
 * @author zero°
 * @since 2023-05-22
 */
@Service
@RequiredArgsConstructor
public class DictApplicationServiceImpl implements DictApplicationService {

    private final DictRepository dictRepository;

    private final DictTypeRepository dictTypeRepository;

    private final DictAssembler dictAssembler;


    @Override
    public boolean saveDict(AddDictCommand command) {
        Dict entity = dictAssembler.formAddDictCommand(command);
        // 持久化
        return dictRepository.saveDict(entity);
    }

    @Override
    public boolean updateDict(UpdateDeptCommand command) {
        Dict entity = dictAssembler.formUpdateDeptCommand(command);
        return dictRepository.updateDict(entity);

    }

    @Override
    public boolean deleteDict(String idsStr) {
        Assert.isTrue(CharSequenceUtil.isNotBlank(idsStr), "删除数据为空");
        //
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong).toList();
        // 删除字典数据项
        return dictRepository.removeByIdList(ids);
    }

    @Override
    public boolean saveDictType(AddDictTypeCommand command) {
        DictType entity = dictAssembler.formAddDictTypeCommand(command);
        // 持久化
        return dictTypeRepository.saveDictType(entity);
    }


    @Override
    public boolean updateDictType(UpdateDictTypeCommand command) {
        // 获取字典类型
        DictType oldDictType = dictTypeRepository.getDictTypeById(command.getId());
        Assert.isTrue(oldDictType != null, "字典类型不存在");
        DictType entity = dictAssembler.formUpdateDeptTypeCommand(command);
        boolean result = dictTypeRepository.updateDictType(entity);
        if (result) {
            // 字典类型code变化，同步修改字典项的类型code
            String oldCode = oldDictType.getCode();
            String newCode = entity.getCode();
            if (!CharSequenceUtil.equals(oldCode, newCode)) {
                dictRepository.updateDictCode(oldCode, newCode);
            }
        }
        return result;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteDictTypes(String idsStr) {
        Assert.isTrue(CharSequenceUtil.isNotBlank(idsStr), "删除数据为空");
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).toList();
        // 删除字典数据项
        List<String> dictTypeCodes = dictTypeRepository.listDictTypeCodeByIds(ids);
        if (CollUtil.isNotEmpty(dictTypeCodes)) {
            dictRepository.removeByTypeCodes(dictTypeCodes);
        }
        // 删除字典类型
        return dictTypeRepository.removeByIdList(ids);
    }

}




